<template>
  <div class='xtx-carousel' @mouseenter="stop" @mouseleave="start">
    <ul class=" carousel-body">
      <li v-for='(item, index) in swipers' :key='item.id' class="carousel-item" :class='{fade: currentIndex === index}'>
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <!-- 左右两侧的箭头 -->
    <a @click='toggle(-1)' href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click='toggle(1)' href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <!-- 底部的圆点 指示器 -->
    <div class="carousel-indicator">
        <!-- v-for如果遍历数字，从1开始 -->
      <span @click='currentIndex=index-1' :class="{active: currentIndex===index-1}" v-for="index in swipers.length" :key="index"></span>
    </div>
  </div>
</template>

<script>
import { onBeforeUnmount, ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 列表数据
    swipers: {
      type: Array,
      required: true
    },
    // 轮播时间间隔
    duration: {
      type: Number,
      default: 1 // 1秒
    },
    // 控制是否自动轮播
    autoplay: {
      type: Boolean,
      default: true
    }
  },
  setup (props) {
    // 当前选中的图片的索引
    const currentIndex = ref(0)

    // 自动轮播功能
    let timer = null
    const autoplayFn = () => {
      timer = setInterval(() => {
        currentIndex.value++
        if (currentIndex.value >= props.swipers.length) {
          // 超出范围后，从头开始
          currentIndex.value = 0
        }
      }, props.duration * 1000)
    }

    // 监听轮播图数据的变化
    watch(() => props.swipers, () => {
      if (props.autoplay && props.swipers.length > 1) {
        autoplayFn()
      }
    }, {
      immediate: true
    })

    // 组件销毁时，清除定时任务
    onBeforeUnmount(() => {
      if (timer) clearInterval(timer)
    })

    // 重启轮播
    const start = () => {
      // 保证定时任务仅仅只能有一个
      if (timer) clearInterval(timer)
      if (props.autoplay && props.swipers.length > 1) {
        autoplayFn()
      }
    }
    // 暂停轮播
    const stop = () => {
      if (timer) clearInterval(timer)
    }

    // 手动切换轮播
    const toggle = (n) => {
      if (n < 0) {
        // 向左切换
        currentIndex.value--
        if (currentIndex.value < 0) {
          // 超出左边界，应该显示最右侧一张
          currentIndex.value = props.swipers.length - 1
        }
      } else {
        // 向右切换
        currentIndex.value++
        if (currentIndex.value >= props.swipers.length) {
          // 超出右边界，应该显示最左侧一张
          currentIndex.value = 0
        }
      }
    }

    return { currentIndex, start, stop, toggle }
  }
}
</script>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
